<?php
set_time_limit(60);
include_once('_path.php');
include_once('_config.php');
include_once(_CONST_LOCAL_INCLUDE_PATH."/top/RequestCheckUtil.php");
include_once(_CONST_LOCAL_INCLUDE_PATH."/top/ItemGetRequest.php");
include_once(_CONST_LOCAL_INCLUDE_PATH."/top/TopClient.php");

$url = trim($_GET['url']);
#过滤白名单
foreach($array as $u)
{
	if(preg_match("#$u#i", $url))
	{
		if(preg_match("#id=([0-9]+)#i", $url, $match))
		{
			$c = new TopClient;
			$c->format = 'json';
			$c->appkey = TB_AKEY;
			$c->secretKey = TB_SKEY;
			$req = new ItemGetRequest;
			$req->setFields("num_iid,title,item_img");
			$req->setNumIid($match[1]);
			$resp = $c->execute($req, $sessionKey);
			
			if(!empty($resp->item->num_iid))
			{
				$title = $resp->item->title;
				foreach($resp->item->item_imgs->item_img as $img)
				{
					$img_array[] = $img->url;
				}
				
				echo json_encode(array(
					's' => 1,
					'title' => $title,
					'url'   => $img_array
				));
				exit;			
			}
			else
			{
					echo json_encode(array('s' => 0,
					'title' => '',
					'url'   => ''));
					exit;
			}			
		}
		else
		{
			echo json_encode(array('s' => 0,
			'title' => '',
			'url'   => ''));
			exit;
		}
	}
}
#其他的url
echo json_encode(getimage($url));
exit;

function getimage($url) {
	$html = GetSources($url,"baiduspider+(+http://www.baidu.com/search/spider.htm)");
	if(!empty($html)){
		//GET TITLE
		preg_match("/<title>(.*)<\/title>/smUi", $html, $s);
		preg_match_all("#<img([^>]*)\s*src=('|\")(http:\/\/([^'\"]+)(\.(jpg|jpeg)))('|\")#i",$html, $m);
		foreach ($m[3] as $val) {
			$t = getimagesize($val);
			if ($t[0]<_CONST_MIN_WIDTH || $t[0]>_CONST_MAX_WIDTH || $t[1]<_CONST_MIN_HEIGHT || $t[1]>_CONST_MAX_HEIGHT) {	
				continue;
			}
			$img_array[] = $val;
		}
		return array(
			's' => 1,
			'title' => trim($s[1]),
			'url'   => $img_array
		);
	}else{
		return array(
			's' => 0,
			'title' => '',
			'url'   => ''
		);
	}	
}

function GetSources($Url,$User_Agent='',$Referer_Url='') //抓取某个指定的页面
{
	//$Url 需要抓取的页面地址
	//$User_Agent 需要返回的user_agent信息 如“baiduspider”或“googlebot”
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $Url);
	curl_setopt ($ch, CURLOPT_USERAGENT, $User_Agent);
	curl_setopt ($ch, CURLOPT_REFERER, $Referer_Url);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	$MySources = curl_exec ($ch);
	curl_close($ch);
	return $MySources;
}
?>